<?

$cnt = 0; $cnt_n = 0; $cnt_u = 0; $cnt_Q = 0; $cnt_i = 0;

$record_count= array();

require('ffnet_sources.php');
/* the above file must contain records like 
ffnet_sources.default.php
*/

/* Grab Em All */
$dataA=array();
while(list($k,$listdat)=each($urllistAll)){
	$url = 'http://www.fanfiction.net/atom/l/'.$listdat[1].'/10/0/1/1/0/0/0/0/0/';
	$metaverse=$listdat[1];
	$cat = $listdat[0];
	$db->Execute("INSERT INTO `fic_metanames` (`universe_id`, `cat_label`) values('".$listdat[1]."','".$cat."')");
	$fdat =  file ( $url );
	$full_data = '';
	if(is_array($fdat)){

		echo count($fdat)." lines in raw data for: ".$url."<br>";
		flush();
		include('ffnetRSS.parse.php');
	}
	//var_dump($dataAts);
	//echo "<hr>";
	$dataA= array_merge($dataA,$dataAts);
	$dataAts=array();

}
//var_dump($dataA);


//exit();


echo "Get All Scan Complete: ".count($id_list)." stories found.<br>";

$sqLST = "SELECT story_id,id,chapters,updated,status FROM fic_listing WHERE sitekey='F' and story_id IN ('".join("','",$id_list)."')";
//echo $sqLST."<br>";
$rsLST = $db->Execute($sqLST);
$aLST = $rsLST->GetAssoc();
//$found = count($dataA);
$poss = count($aLST);
echo $poss." Get All stories in potential updates<br>";
//exit();

while(list($key,$val)=each($dataA)){
	if(isset($aLST[$val['id']])){		
				$upd = false;
				if($aLST[$val['id']]['chapters']<$val['chapters']){ $upd = true; }
				if($aLST[$val['id']]['updated']!=$val['updated']){ $upd = true; }
				$record_count['total']=$record_count['total']+1;
				$insid = $aLST[$val['id']]['id'];
				$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='".$val['metaverse']."'");

				if($upd){
					$adQ = '';
					$sqT = "chapters='".$val['chapters']."',updated='".$val['updated']."',inserted=NOW()";
						$sqT .= ", summary='".str_replace("'","''",$val['summary'])."'";	
						$sqT .= ", story_title='".str_replace("'","''",$val['title'])."'";	
					

					if($val['updated']!='20--'){					
						if($aLST[$val['id']]['status']=='?' || $aLST[$val['id']]['status']=='N'){
							$record_count['updated']=$record_count['updated']+1;
							$sqlU = "UPDATE fic_listing SET ".$sqT.",`inserted`=NOW() WHERE sitekey='F' and story_id='".$val['id']."'";
							
							$ffUD = $ffUD+1;
						}elseif($aLST[$val['id']]['status']!='I' && $aLST[$val['id']]['status']!='B' ){	
							$record_count['updated']=$record_count['updated']+1;
							$sqlU = "UPDATE fic_listing SET ".$sqT.",status='U',`inserted`=NOW() WHERE sitekey='F' and story_id='".$val['id']."'";						
							
							$ffUD = $ffUD+1;
						}elseif($aLST[$val['id']]['status']=='I'){
								$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='F' and story_id='".$val['id']."'";										
$record_count['ignored']=$record_count['ignored']+1;			
						}elseif( $aLST[$val['id']]['status']=='D' || $aLST[$val['id']]['status']=='B'){	
								$cnt1=$cnt1+1;
								$sqlU = "UPDATE fic_listing SET ".$sqT.",status='?',`inserted`=NOW() WHERE sitekey='F' and story_id='".$val['id']."'";						
$record_count['ignored']=$record_count['ignored']+1;

								$ffUD = $ffUD+1;
						}
//echo $sqlU."<br>";
						
						$db->Execute($sqlU);
						
						
					}else{
					}
				}else{
				}			
	}else{
		$sql = "INSERT INTO `fic_listing` ( `story_id` , `author_id` , `story_title` , `chapters` , `published` , `updated` , `status`,`summary`,`inserted`,sitekey ) VALUES ('".$val['id']."', '".$val['author_id']."', '".str_replace("'","''",$val['title'])."', '".$val['chapters']."', '".$val['published']."', '".$val['updated']."', '?','".str_replace("'","''",$val['summary'])."', NOW(),'F')";
		$cnt2 = $cnt2 + 1;
$record_count['new']=$record_count['new']+1;
//echo $sql."<br>";
		$rs1 = $db->Execute($sql);

		$insid = $db->Insert_ID();
		$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='".$val['metaverse']."'");
		
		$sql2 = "INSERT INTO `fic_authors` ( `author_id` , `author_name`,sitekey )VALUES ('".$val['author_id']."', '".str_replace("'","''",$val['author'])."','F')";
//echo $sql2."<br>";

		$rs2 = $db->Execute($sql2);

	}
}



//exit();

/* Grab Tracked Ones */
$dataA = array(); $id_list= array();
while(list($k,$listdat)=each($urllistTrack)){
	$url = 'http://www.fanfiction.net/atom/l/'.$listdat[1].'/10/0/1/1/0/0/0/0/0/';
	$metaverse=$listdat[1];
	$cat = $listdat[0];
	$db->Execute("INSERT INTO `fic_metanames` (`universe_id`, `cat_label`) values('".$listdat[1]."','".$cat."')");
	$fdat =  file ( $url );
	$full_data = '';
	if(is_array($fdat)){

		echo count($fdat)." lines in raw data for: ".$url."<br>";
		flush();
		include('ffnetRSS.parse.php');
	}
	//var_dump($dataAts);
	//echo "<hr>";
	$dataA= array_merge($dataA,$dataAts);
	$dataAts=array();


}

echo "Get Updates Scan Complete: ".count($id_list)." stories found.<br>";

$sqLST = "SELECT story_id,id,chapters,updated,status FROM fic_listing WHERE sitekey='F' and story_id IN ('".join("','",$id_list)."')";
//echo $sqLST."<br>";
$rsLST = $db->Execute($sqLST);
$aLST = $rsLST->GetAssoc();
//$found = count($dataA);
$poss = count($aLST);
echo $poss." Get update stories in potential updates<br>";

while(list($key,$val)=each($dataA)){
	if(isset($aLST[$val['id']])){		

				$upd = false;
				if($aLST[$val['id']]['chapters']<$val['chapters']){ $upd = true; }
				if($aLST[$val['id']]['updated']!=$val['updated']){ $upd = true; }
				$record_count['total']=$record_count['total']+1;
				$insid = $aLST[$val['id']]['id'];
				$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='".$val['metaverse']."'");

				if($upd){
					$adQ = '';
					$sqT = "chapters='".$val['chapters']."',updated='".$val['updated']."',inserted=NOW()";
						$sqT .= ", summary='".str_replace("'","''",$val['summary'])."'";	
						$sqT .= ", story_title='".str_replace("'","''",$val['title'])."'";	
					

					if($val['updated']!='20--'){					
						if($aLST[$val['id']]['status']=='?' || $aLST[$val['id']]['status']=='N'){
							$record_count['updated']=$record_count['updated']+1;
							$sqlU = "UPDATE fic_listing SET ".$sqT.",`inserted`=NOW() WHERE sitekey='F' and story_id='".$val['id']."'";
							
							$ffUD = $ffUD+1;
						}elseif($aLST[$val['id']]['status']!='I' && $aLST[$val['id']]['status']!='B' ){	
							$record_count['updated']=$record_count['updated']+1;
							$sqlU = "UPDATE fic_listing SET ".$sqT.",status='U',`inserted`=NOW() WHERE sitekey='F' and story_id='".$val['id']."'";						
							
							$ffUD = $ffUD+1;
						}elseif($aLST[$val['id']]['status']=='I'){
								$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='F' and story_id='".$val['id']."'";										
$record_count['ignored']=$record_count['ignored']+1;			
						}elseif( $aLST[$val['id']]['status']=='D' || $aLST[$val['id']]['status']=='B'){	
								$cnt1=$cnt1+1;
								$sqlU = "UPDATE fic_listing SET ".$sqT.",status='?',`inserted`=NOW() WHERE sitekey='F' and story_id='".$val['id']."'";						
$record_count['ignored']=$record_count['ignored']+1;

								$ffUD = $ffUD+1;
						}
//echo $sqlU."<br>";
						
						$db->Execute($sqlU);
						
						
					}else{
					}
				}else{
				}			

	}
}


echo '&nbsp;&nbsp;&nbsp;Update Completed<br>';
echo '&nbsp;&nbsp;&nbsp;total entries:'.$record_count['total'].', ignored:'.$record_count['ignored'].', new stories:'.$record_count['new'].',  UPDATED:'.$record_count['updated'].'<br>';

$db->Execute("UPDATE fic_authors SET linkid=id WHERE linkid='0'");

?>
